/*
----------------------------------------------------------------------------------------------------
This source file is part of the Gamut Framework
Copyright (c) 2007 by Ryan Holcombe

This program is free software; you can redistribute it and/or modify it under the terms of the GNU
Lesser General Public License as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
----------------------------------------------------------------------------------------------------
*/


#ifndef _Console_h_
#define _Console_h_

#include "Singleton.h"
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <map>
#include <vector>
#include <list>


/** registers a function, pass in command name, class function address, and a pointer to the class object
*/
#define CONSOLE_REGISTER(name, fcnaddr, ptr) \
	assert(Gamut::Console::getSingletonPtr()); \
	Gamut::Console::getSingleton().registerCallback(name, boost::bind(fcnaddr, ptr, _1))

/** removes a function reference, pass in the command name
*/
#define CONSOLE_REMOVE(name) \
	assert(Gamut::Console::getSingletonPtr()); \
	Gamut::Console::getSingleton().removeCallback(name)

/** executes a command
*/
#define CONSOLE_EXECUTE(command) \
	assert(Gamut::Console::getSingletonPtr()); \
	Gamut::Console::getSingleton().executeCallback(command)


namespace Gamut
{

	/**
 		Console allows for in-game debugging via text input

		\remarks
			Functions are registered to the class along with a name. To invoke a function, call the name
			along with any parameters necessary for it to execute (i.e. setHealth 100)\n
			Arguments are stored in vector format so each function registered must be in the form of
			bool functionName(std::vector<std::string>)
	*/
	
	typedef std::vector<std::string> ConsoleArgs;
	typedef boost::function<bool (ConsoleArgs)> ConsoleCallback;
	typedef std::map<std::string, ConsoleCallback> CallbackMap;
	typedef std::list<std::string> CallbackList;

	class Console : public Singleton<Console>
	{
	private:
		//! holds a list of all registered functions
		CallbackMap m_callback;		

	public:
		/** Default Constructor
		*/
		Console();
		
		
		/** Default Deconstructor
		*/
		~Console();
		
			
		/**
			\brief registers a callback command with the class and is involked with the passed in name
			\return 0 if the name is already in the map
		*/
		virtual bool registerCallback(std::string name, ConsoleCallback fptr);
		
		
		/**
			\brief removes a callback command from the map
		*/
		virtual void removeCallback(std::string name);
		
		
		/**
			\brief parses the string command and executes the appropriate callback function
			\return the return value of the callback function
		*/
		virtual bool executeCallback(std::string command);

		/**
			\brief returns a list of callback functions currently registered
			\return a list of registered functions as a type Gamut::CallbackList
		*/
		virtual CallbackList getCallbackList();
	};
	
}



#endif
